# rm(list = ls());
gc()

# source("00_pckgs.R")

sessions <- 79:115

rc_res <- lapply(sessions, function(x) voteview_search(congress = x, chamber = "Senate", minsupport = 5, maxsupport = 95))

members_res <- lapply(sessions, function(x) member_search(congress = x, chamber = "Senate"))

for (s in 1:(length(sessions))){
  dir.create(paste0("data/us/", sessions[s]), recursive = TRUE)
  vw <- voteview_download(rc_res[[s]]$id)
  rc <- vw$votes
  rc[rc %in% c(vw$codes$notInLegis,vw$codes$missing)] <- NA
  rc[rc %in% vw$codes$yea] <- 1
  rc[rc %in% vw$codes$nay] <- 0
  rc <- rc[apply(rc, 1, function(x) ncol(rc) - sum(is.na(x))) > 9,]
  rc <- rc[,apply(rc, 2, function(x) nrow(rc) - sum(is.na(x))) > 9]
  rc_desc <- rc_res[[s]]
  mem_desc <- members_res[[s]]
  rc <- rc[rownames(rc) %in% as.character(mem_desc$icpsr),] # kick out president and vicepresident
  save(rc, rc_desc, mem_desc,
       file = paste0("data/us/", sessions[s], "/raw.Rda"))
  gc();Sys.sleep(5);gc()
}
